import {
  defineConfig,
  loadEnv,
  splitVendorChunkPlugin,
  PluginOption
} from 'vite';
import path from 'path';
import { createVuePlugin } from 'vite-plugin-vue2';
import { createSvgPlugin } from 'vite-plugin-vue2-svg';
import { visualizer } from 'rollup-plugin-visualizer';
// import { viteMockServe } from 'vite-plugin-mock';
import eslint from 'vite-plugin-eslint';
// import legacy from '@vitejs/plugin-legacy';
import { createHtmlPlugin } from 'vite-plugin-html';
import Package from './package.json';

// eslint-disable-next-line @typescript-eslint/no-unused-vars
export default defineConfig(({ command, mode }) => {
  const CWD = process.cwd();
  const { VITE_BASE_URL } = loadEnv(mode, CWD, '');
  return {
    base: VITE_BASE_URL, // 设置打包路径
    server: {
      host: '0.0.0.0',
      // port: 10086, // 设置服务启动端口号
      open: false, // 设置服务启动时是否自动打开浏览器
      cors: true // 允许跨域

      // 设置代理，根据项目实际情况配置
      // proxy: {
      // }
    },
    css: {
      modules: {
        localsConvention: 'camelCase' // 默认只支持驼峰，修改为同时支持横线和驼峰
      },
      // CSS 预处理器
      preprocessorOptions: {
        // 定义全局SCSS变量
        scss: {
          javascriptEnabled: true,
          additionalData: `@import "@/styles/mixins.scss";@import "@/styles/variables.scss";`
        }
      }
    },
    build: {
      minify: 'terser',
      terserOptions: {
        compress: {
          pure_funcs: ['console.log'],
          // drop_console: true, // 生产环境去除console
          drop_debugger: true // 生产环境去除debugger
        }
      },
      rollupOptions: {
        output: {
          manualChunks(id) {
            if (/[\\/]node_modules[\\/]_?lodash[\\/]/.test(id)) {
              return 'lodash';
            }
          }
        }
      }
    },
    plugins: [
      createVuePlugin({ jsx: true }),
      // viteMockServe({
      //   mockPath: 'mock',
      //   watchFiles: true,
      //   localEnabled: command === 'serve'
      // }),
      createHtmlPlugin({
        inject: {
          data: {
            title: Package.name,
            keywords: Package.keywords.join(),
            description: Package.description,
            author: Package.author,
            injectScript: `<script>window.version = "${Package.version}";</script>`
          }
        }
      }),
      // legacy(),
      eslint({
        exclude: ['**/node_modules/**', 'dist/**', 'public/**']
      }),
      splitVendorChunkPlugin(),
      createSvgPlugin({
        svgoConfig: {
          plugins: [
            {
              name: 'preset-default',
              params: {
                overrides: {
                  // customize default plugin options
                  inlineStyles: {
                    onlyMatchedOnce: false
                  },

                  // or disable plugins
                  removeDoctype: false
                }
              }
            },
            'convertStyleToAttrs', // 将样式转换为属性
            'removeStyleElement', // 删除 <style> 元素
            'removeTitle',
            'removeComments',
            'removeDesc',
            'removeUselessDefs',
            'convertShapeToPath' // 将一些基本形状转换为 < path >
          ]
        }
      }),
      visualizer({
        emitFile: false,
        gzipSize: true,
        filename: 'report.html'
      })
    ].filter(Boolean) as PluginOption[],
    optimizeDeps: {
      include: ['vue', 'vue-router', 'axios', 'path-to-regexp']
    },
    resolve: {
      alias: {
        '@': path.resolve(__dirname, 'src')
      },
      extensions: ['.vue', '.ts', '.tsx', '.js', 'jsx', '.json']
    }
  };
});
